package com.itheima.restkeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.restkeeper.mapper.ApicertMapper;
import com.itheima.restkeeper.pojo.Apicert;
import com.itheima.restkeeper.req.ApicertVo;
import com.itheima.restkeeper.service.IApicertService;
import com.itheima.restkeeper.utils.BeanConv;
import com.itheima.restkeeper.utils.EmptyUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description： 服务实现类
 */
@Service
public class ApicertServiceImpl extends ServiceImpl<ApicertMapper, Apicert> implements IApicertService {


    @Override
    public Page<Apicert> findApicertVoPage(ApicertVo apicertVo, int pageNum, int pageSize) {
        //构建分页对象
        Page<Apicert> page = new Page<>(pageNum,pageSize);
        //构建查询条件
        QueryWrapper<Apicert> queryWrapper = new QueryWrapper<>();
        //按类型查询
        if (!EmptyUtil.isNullOrEmpty(apicertVo.getCertType())) {
            queryWrapper.lambda().likeRight(Apicert::getCertType,apicertVo.getCertType());
        }
        //按是否过去状态查询
        if (!EmptyUtil.isNullOrEmpty(apicertVo.getEnableStatus())) {
            queryWrapper.lambda().likeRight(Apicert::getEnableStatus,apicertVo.getEnableStatus());
        }
        //按是否有效状态查询
        if (!EmptyUtil.isNullOrEmpty(apicertVo.getEnableFlag())) {
            queryWrapper.lambda().eq(Apicert::getEnableFlag,apicertVo.getEnableFlag());
        }
        //按创建时间降序
        queryWrapper.lambda().orderByDesc(Apicert::getCreatedTime);
        //执行分页查询
        return page(page, queryWrapper);
    }

    @Override
    public Apicert createApicert(ApicertVo apicertVo) {
        //转换ApicertVo为Apicert
        Apicert apicert = BeanConv.toBean(apicertVo, Apicert.class);
        boolean flag = save(apicert);
        if (flag){
            return apicert;
        }
        return null;
    }

    @Override
    public Boolean updateApicert(ApicertVo apicertVo) {
        //转换ApicertVo为Apicert
        Apicert apicert = BeanConv.toBean(apicertVo, Apicert.class);
        return updateById(apicert);
    }

    @Override
    public Boolean deleteApicert(String[] checkedIds) {
        //转换数组为集合
        List<String> ids = Arrays.asList(checkedIds);
        List<Long> idsLong = new ArrayList<>();
        ids.forEach(n->{
            idsLong.add(Long.valueOf(n));
        });
        return removeByIds(idsLong);
    }

}
